﻿using System;
using System.Net;
using System.Linq;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Ayatta.Contract.Domain;
using Web.Base.Extensions;
using Web.Manage.Controllers;
using Ayatta.Contract.Extension;
using System.Collections.Generic;
using Web.Manage.Areas.Game.Models;

namespace Web.Manage.Areas.Game.Controllers
{
    public class TrialController : AdminBaseController
    {

        public TrialController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index(int serverId = 0)
        {
            var model = new TrialControllerModel.Index();
            var games = GetGames();
            model.ServerId = serverId;
            model.Games = games;
            if (serverId > 0)
            {
                model.Trials = GameService.GetGameTrials(serverId);
                model.Server = games.GetAllServers().FirstOrDefault(o => o.Id == serverId);
            }
            else
            {
                model.Trials = new List<GameTrial>();
            }
            return PartialView(model);
        }

        public ActionResult Detail(int? param, int serverId = 0)
        {
            var model = new TrialControllerModel.Detail();
            var games = GetGames();
            model.Games = games;
            model.ServerId = serverId;
            var server = games.GetAllServers().FirstOrDefault(o => o.Id == serverId);
            var gameId = 0;
            if (server != null)
            {
                gameId = server.GameId;
            }
            model.Trial = new GameTrial { ServerId = serverId, GameId = gameId };
            if (param.HasValue && param.Value > 0)
            {
                model.Trial = GameService.GetGameTrial(param.Value);
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Detail(int? param, int gameId, int serverId, GameTrial model)
        {
            var time = DateTime.Now;
            var userName = User.Identity.Name;

            if (model.Level < 1 || model.Integral < 1)
            {
                return Json(HttpStatusCode.BadRequest, "等级和积分必须大于0");
            }

            if (param.HasValue && param.Value > 0)
            {
                var old = GameService.GetGameTrial(param.Value);
                if (TryUpdateModel(old))
                {
                    var result = GameService.UpdateGameTrial(old);
                    return result ? Json(HttpStatusCode.OK, "更新成功", true, "/game/trial", "/game/trial?serverId=" + serverId) : Json(HttpStatusCode.BadRequest, "更新失败", "/game/home/servers/", "/game/home/servers/" + gameId);
                }
            }
            else
            {
                model.GameId = gameId;
                model.ServerId = serverId;
                model.CreatedBy = userName;
                model.CreatedOn = time;
                var result = GameService.CreateGameTrial(model);
                if (result)
                {
                    return Json(HttpStatusCode.OK, "添加成功", true, "/game/trial", "/game/trial?serverId=" + serverId);
                }
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

        public ActionResult History(int page = 1, int size = 50, int? userId = null, int? serverId = null, int? level = null, DateTime? date = null)
        {
            var model = new TrialControllerModel.History();
            var games = GetGames();
            model.Level = level;
            model.UserId = userId;
            model.ServerId = serverId;
            model.Date = date;
            model.UserId = userId;
            model.Games = games;
            model.Servers = games.GetAllServers(true);
            model.Histories = GameService.GetPagedGameTrialHistories(page, size, userId, serverId, level, date).ToPagedList(page, size);
            return PartialView(model);
        }
    }
}
